package com.example.blogdemo.repository;

import com.example.blogdemo.entity.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2017-12-15
 **/
public interface TagRepository extends JpaRepository<Tag, Integer> {

    Tag findByName(String name);

    List<Tag> findAll();

    @Query(value = "select count(id) from tag",nativeQuery = true)
    Integer findTagCount();

    @Transactional
    @Modifying
    @Query(value = "update tag t set t.count = ?2 where t.id = ?1",nativeQuery = true)
    void updateCountByTagId(Integer tagId, Integer count);

    @Transactional
    @Modifying
    @Query(value = "delete from tag where id =:id",nativeQuery = true)
    void deleteById(@Param("id") Integer id);

    List<Tag> findByUserId(Integer userId);
}


